<?php
/*gugod class*/
class CI_Datetime {

	var $lang;
	 
	/*รูปแบบการใช้งาน
	date_format("วันที่จาก database","รูปแบบ","ปีที่บวกเพิ่ม","กำหนดค่าที่จะให้แสดง")
	*/
	function __construct()
	{
		$this->lang = array("0"=>"en","543"=>"th");	
	}
	
	function format($data,$style='shortdate',$year=0,$param="") 
	{
		if($data!="") :
			$datetime = date("Y-m-d H:i:s",strtotime($data));
			list($dates,$times) = explode(" ",$datetime);
			$year = intval($year,10);
			list($date['y'],$date['m'],$date['d']) = explode("-",$dates);
			$date['y'] = ($date['y']+$year);
			list($time['h'],$time['i'],$time['s']) = explode(":",$times);
			switch($style) :
				case'shortdate': return  $this->shortdate($date,$param); break;
				case'shorttime': return  $this->shorttime($time,$param); break;
				case'shortdatetime': return  $this->shortdate($date,$param)." ".$this->shorttime($time,$param); break;
				case'minidate': return  $this->minidate($date,$param,$year); break;
				case'fulldate': return  $this->fulldate($date,$param,$year); break;
				case'minidatetime': return  $this->minidate($date,$param,$year)." ".$this->shorttime($time,$param); break;
				case'fulldatetime': return  $this->fulldate($date,$param,$year)." ".$this->shorttime($time,$param); break;
			endswitch;
		endif;
	}
	
	function shortdate($date,$param="")
	{
		if($param=="" || $param=="dmy") :
			return $date['d']."-".$date['m']."-".$date['y'];
		elseif($param=="" || $param=="my") :
			return $date['m']."-".$date['y'];
		elseif($param=="d"):
			return $time['d'];
		elseif($param=="m") :
			return $time['m'];
		elseif($param=="y"):
			return $time['y'];
		endif;
	}
	
	function minidate($date,$param="",$year=0)
	{
		$lang = $this->lang[$year];
		if($param=="" || $param=="dmy") :
			return $date['d']." ".$this->month($date['m'],$lang,"short")." ".$date['y'];
		elseif($param=="my") :
			return $this->month($date['m'],$lang,"short")." ".$date['y'];
		elseif($param=="d"):
			return $date['d'];
		elseif($param=="m") :
			return $this->month($date['m'],$this->lang,"short");
		elseif($param=="y"):
			return $date['y'];
		endif;
	}
	
	function fulldate($date,$param="",$year=0)
	{
		$lang = $this->lang[$year];
		if($param=="" || $param=="dmy") :
			return $date['d']." ".$this->month($date['m'],$lang,"full")." ".$date['y'];
		elseif($param=="my") :
			return $this->month($date['m'],$lang,"full")." ".$date['y'];
		elseif($param=="d"):
			return $date['d'];
		elseif($param=="m") :
			return $this->month($date['m'],$this->lang,"full");
		elseif($param=="y"):
			return $date['y'];
		endif;
	}
	
	function shorttime($time,$param="")
	{
		if($param=="" || $param=="hi") :
			return $time['h'].":".$time['i'];
		elseif($param=="his"):
			return $time['h'].":".$time['i'].":".$time['s'];	
		elseif($param=="h"):
			return $time['h'];
		elseif($param=="i"):
			return $time['i'];
		elseif($param=="s"):
			return $time['s'];
		endif;
	}
	
 	function month($month,$lang='en',$size='full')	
	{	
		$month_th_full = array('','มกราคม','กุมภาพันธ์','มีนาคม','เมษายน', 'พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤษจิกายน','ธันวาคม');
		$month_th_short = array('','ม.ค.','ก.พ.','มี.ค.','เม.ย.', 'พ.ค.','มิ.ย.','ก.ค.','ส.ค.','ก.ย.','ต.ค.','พ.ย.','ธ.ค.');
		$month_en_full = array("","January","February","March","April","May","June","July","August","September","October","November","December");
		$month_en_short = array("","Jan","Feb","March","Apr","May","Jun","July","Aug","Sep","Oct","Nov","Dec");
		$month=($month+0);
		switch($lang) :
			case'en':
				if($size=="short") :
					return $month_en_short[$month];
				else :
					return $month_en_full[$month];
				endif;
			break;
			
			case'th':
				if($size=="short") :
					return $month_th_short[$month];
				else :
					return $month_th_full[$month];
				endif;
			break;
		endswitch;
	}
	
	function day($day,$lang='en',$size='full')	
	{	
		$day_th_full = array('','จันทร์','อังคาร','พุธ','พฤหัสบดี', 'ศุกร์','เสาร์','อาทิตย์');
		$day_th_short =array('','จ','อ','พ','พฤ', 'ศ','ส','อา');
		$day_en_full = array("","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
		$day_en_short = array("","Mon","Tue","Wed","Thurs","Fri","Sat","Sun");
		$day=($day+0);
		switch($lang) :
			case'en':
				if($size=="short") :
					return $mday_en_short[$day];
				else :
					return $day_en_full[$day];
				endif;
			break;
			
			case'th':
				if($size=="short") :
					return $day_th_short[$day];
				else :
					return $day_th_full[$day];
				endif;
			break;
		endswitch;
	}

	function getAge($birthday) 
	{  
			 $then = strtotime($birthday);  
			 return(floor((time()-$then)/31556926));  
	 }  

}

?>